import 'package:flutter/material.dart';

import '../x3_charts_core.dart';

///**
///* @fileName: rounded_rectangle_chart_painter
///* @author: hrongyong
///* @date-time: 3/16/21 9:01 AM
///* @email: hrongyong@gmail.com
///* description: 矩形图小部件
///* <fileName> <author> <date-time> <email> <description>
///* 文件名 作者姓名 创建时间 作者邮箱 文件描述
///*/

class RectangleChartPainter extends CustomPainter {
  ///图表数据
  final List<GeneralChartModel> chartList;

  ///动画数值
  final Animation<double> repaint;

  ///x轴文本描述大小
  final double xAxisSize;

  ///背景颜色
  final Color backgroundColor;

  ///是否开启圆角
  final bool turnOnFillet;

  RectangleChartPainter({
    @required this.chartList,
    @required this.repaint,
    @required this.xAxisSize,
    this.backgroundColor = Colors.white,
    this.turnOnFillet = true,
  }) : super(repaint: repaint);

  ///间距
  EdgeInsets chartPadding;

  ///底部边距
  double bottomMargin = 0.0;

  final TextPainter _textPainter = TextPainter(textDirection: TextDirection.ltr); //文字画笔

  double maxWidth = 0.0;
  double maxHeight = 0.0;
  double xStep = 0; // x 间隔
  double yStep = 0; // y 间隔

  //直方图的填充颜色
  Paint fillPaint = Paint()..color = Colors.red;

  @override
  void paint(Canvas canvas, Size size) {
    if (chartList == null || chartList.length == 0) return;

    //Y轴系数
    TextPainter painter01 = TextPainter(
        maxLines: 1,
        textDirection: TextDirection.ltr,
        text: TextSpan(
            text: "test",
            style: TextStyle(
              fontSize: xAxisSize,
              fontWeight: FontWeight.bold,
            )));
    painter01.layout();

    ///确定边距
    chartPadding = EdgeInsets.only(left: 10, right: 10.0, top: 10.0 + painter01.height * 2, bottom: 10.0 + painter01.height);

    ///可绘画的宽度
    maxWidth = size.width - chartPadding.left - chartPadding.right;

    ///x、y轴的大小
    /// x 间隔
    xStep = maxWidth / chartList.length;

    ///可绘画的高度
    maxHeight = size.height - chartPadding.top - chartPadding.bottom;
    yStep = (size.height - chartPadding.bottom - chartPadding.top) / 100;

    ///绘制背景
    canvas.drawRect(Offset.zero & size, Paint()..color = backgroundColor);

    ///移动绘画中心
    canvas.translate(chartPadding.left, size.height - chartPadding.bottom);

    ///绘制X轴刻度和文本
    drawXAxisScaleAndText(canvas, size);

    ///绘制直方图和描述
    drawHistogramAndDescription(canvas, size);
  }

  drawHistogramAndDescription(Canvas canvas, Size size) {
    canvas.save();
    canvas.translate(xStep / 2, 0);
    for (int i = 0; i < chartList.length; i++) {
      if (chartList[i].firstHistogramColor != null) fillPaint..color = chartList[i].firstHistogramColor;
      double height = -chartList[i].firstHistogram * maxHeight;

      ///绘制直方
      if (turnOnFillet) {
        RRect rrect = RRect.fromLTRBR(-xStep * 0.4, 0, xStep * 0.4, height * repaint.value, Radius.circular(4));
        canvas.drawRRect(rrect, fillPaint);
      } else {
        Rect rect = Rect.fromLTRB(-xStep * 0.4, 0, xStep * 0.4, height * repaint.value);
        canvas.drawRect(rect, fillPaint);
      }

      ///绘制文本描述
      _draDescriptionText(
        canvas,
        title: chartList[i].firstHistogramDescription,
        subTitle: chartList[i].secondHistogramDescription,
        chartHistogram: chartList[i].firstHistogram,
        offset: Offset(0, height * repaint.value),
        index: i,
      );
      canvas.translate(xStep, 0);
    }
    canvas.restore();
  }

  ///绘制直方图顶部文字
  void _draDescriptionText(
    Canvas canvas, {
    String title,
    String subTitle,
    double chartHistogram,
    Offset offset = Offset.zero,
    Color color = Colors.black,
    int index,
  }) {
    ///绘制标题
    TextSpan text01 = TextSpan(text: title, style: TextStyle(fontSize: xAxisSize, color: color));
    _textPainter.text = text01;
    _textPainter.layout(); // 进行布局
    Size textSize01 = _textPainter.size;
    Offset textOffsetPos01;
    double _height = chartList[index].firstHistogram * maxHeight;
    if (chartHistogram == 0) {
      textOffsetPos01 = Offset(offset.dx - textSize01.width / 2, offset.dy - textSize01.height);
    } else if (chartHistogram > 0 && chartHistogram < 0.2) {
      textOffsetPos01 = Offset(offset.dx - textSize01.width / 2, offset.dy + _height / 2 - textSize01.height / 2);
    } else {
      textOffsetPos01 = Offset(offset.dx - textSize01.width / 2, offset.dy + textSize01.height);
    }

    _textPainter.paint(canvas, textOffsetPos01);
    TextSpan text02 = TextSpan(text: subTitle, style: TextStyle(fontSize: xAxisSize, color: color));
    _textPainter.text = text02;
    _textPainter.layout(); // 进行布局
    Size textSize02 = _textPainter.size;
    Offset textOffsetPos02;
    if (chartHistogram == 0) {
      textOffsetPos02 = Offset(offset.dx - textSize02.width / 2, offset.dy - textSize01.height - textSize02.height);
    } else {
      textOffsetPos02 = Offset(offset.dx - textSize02.width / 2, offset.dy - textSize02.height - 5);
    }
    _textPainter.paint(canvas, textOffsetPos02);
  }

  drawXAxisScaleAndText(Canvas canvas, Size size) {
    canvas.save();

    ///x轴线
    canvas.translate(xStep, 0);

    for (int i = 0; i < chartList.length; i++) {
      ///x轴线描述
      _drawXAxisText(canvas, title: chartList[i].name, offset: Offset(-xStep / 2, 0), color: Colors.black54);
      _drawXAxisText(canvas, title: chartList[i].thirdHistogramDescription, offset: Offset(-xStep / 2, 15), color: Colors.black54);
      canvas.translate(xStep, 0);
    }
    canvas.restore();
  }

  ///绘制X轴文字
  void _drawXAxisText(
    Canvas canvas, {
    String title,
    Offset offset = Offset.zero,
    Color color = Colors.black,
  }) {
    ///绘制标题
    TextSpan text = TextSpan(text: title, style: TextStyle(fontSize: xAxisSize, color: color));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局
    Size textSize = _textPainter.size;
    Offset textOffsetPos = Offset(offset.dx - textSize.width / 2, offset.dy);
    _textPainter.paint(canvas, textOffsetPos);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
